const { json } = require('express');
var express = require('express');
var router = express.Router();
const query = require('../bd/query')

const {htmlEncodeByRegExp,htmlDecodeByRegExp,html2Escape,escape2Html} = require('../tool/EncodeHtml')//使用html编码


/* GET users listing. */
router.get('/', function(req, res, next) {
  
  res.send('欢迎来到源博客');
});
//SELECT * FROM article LEFT JOIN user ON article.loginId=user.loginId WHERE user.loginId=123456 LIMIT 1,2
// 获取
router.post('/blog/list',function(req,res,next){
    var begin=req.body.begin;//开始位置
    var end=req.body.end;//结束位置
    var mothed=req.body.mothed
    var sql;
    if(typeof(mothed)=="number"){//我的博客
        sql=`SELECT * FROM article LEFT JOIN user 
            ON article.loginId=user.loginId 
            WHERE user.loginId=${mothed} 
            order by article.createtime desc 
            LIMIT ${begin},${end}`
    } 
    else if(mothed=="all"){//全部博客内容
        sql=`SELECT * FROM article LEFT JOIN user 
            ON article.loginId=user.loginId 
            order by article.createtime desc 
            LIMIT ${begin},${end}`
    }
    else if(typeof(mothed)=="string"){//搜索
        sql=`SELECT * FROM article LEFT JOIN user 
            ON article.loginId=user.loginId WHERE content like "%${mothed}%" 
            order by article.createtime desc 
            LIMIT ${begin},${end}`
    }
    else if(typeof(mothed)=="undefined"){
        res.send("报错了")
    }
    // console.log(begin,end,mothed)
    query(sql)
    .then((r)=>{
        res.send( JSON.stringify(r))
    })
    .catch(()=>{
        res.send(JSON.stringify({msg:"无法查找博客内容"}))
    });
})
//博客一共多少条
router.post('/blog/total',function(req,res,next){
    var mothed=req.body.mothed
    console.log(mothed+"23333")
    var sql;
    console.log(typeof(mothed)+":"+mothed)
    if(typeof(mothed)=="number"){//我的博客
        sql=`SELECT count(*) as counts FROM article LEFT JOIN user 
            ON article.loginId=user.loginId 
            WHERE user.loginId=${mothed} 
            `
    } 
    else if(mothed=="all"){//全部博客内容
        sql=`SELECT count(*) as counts FROM article LEFT JOIN user 
            ON article.loginId=user.loginId `
    }
    else if(typeof(mothed)=="string"){//搜索
        sql=`SELECT count(*) as counts FROM article LEFT JOIN user 
            ON article.loginId=user.loginId WHERE content like "%${mothed}%" `
    }
    else if(typeof(mothed)=="undefined"){
        res.send("报错了")
    }
    console.log(mothed)
    query(sql)
    .then((r)=>{

        res.send( JSON.stringify(r))
    })
    .catch(()=>{
        res.send(JSON.stringify({msg:"无法查找博客内容"}))
    });
})
/**
 * 查询到单个博客内容
 */
router.post('/blog/item',function(req,res,next){
    var id = req.body.id
    var sql = `SELECT * FROM article LEFT JOIN user 
                ON article.loginId=user.loginId 
                WHERE article.id=${id} `
    query(sql)
    .then(r=>{
        // console.log(r)
        res.send(r)
    })
    .catch(r=>{
        res.send(JSON.stringify({status:'no',msg:"查询单个博客内容,连接失败"}))
    })
})


// 增加
router.post('/blog/add',function(req, res, next){
    var title=req.body.title//标题
    var content = req.body.content//内容
    var loginId = req.body.loginId//用户ID
    var date = Math.round(new Date() / 1000)//时间
    // console.log(htmlEncodeByRegExp)
    content=htmlEncodeByRegExp(content)
    // console.log(content)
    let sql= `INSERT INTO article (title,content,loginId,createtime) VALUES ("${title}","${content}",${loginId},${date})`
    console.log(sql)
    query(sql)
    .then((r)=>{
        console.log("成功")
        console.log(r.affectedRows)
        if(r.affectedRows==1){
          res.end(JSON.stringify({msg:"写博客成功",status:"yes"}))
        }
        else{
          res.end(JSON.stringify({msg:"写博客失败",status:"yes"}))
        }
    
    })
    .catch((r)=>{res.end(JSON.stringify({msg:"写博客-连接失败",status:"no"}))})
})
// 删除
.post('/blog/del',function(req,res,next){
    var id = req.body.id;
    var loginId = req.body.loginId
    let sql = `delete from article where id=${id} and loginId=${loginId}`
    query(sql).then((result)=>{
        if(result.affectedRows>0)
        {
            res.send("success")
        }

    })
})
// 修改
.post('/blog/update',function(req,res){
    // console.log(req)
    var title =req.body.title
    var content=req.body.content
    var id =req.body.id
    // title=htmlEncodeByRegExp(title)
    content=htmlEncodeByRegExp(content)
    let sql=`update article set title="${title}",
            content="${content}"
            where id=${id}`
    // console.log(sql)
    query(sql)
    .then((resule)=>{
        if(resule.affectedRows==1)
        {
          res.end(JSON.stringify({status:"ok",msg:"博客内容-修改成功"}))
        }
        else
        {
          res.end('博客内容-修改失败')
        }
      })
      .catch(r=>{res.end({status:"no",msg:"博客内容-修改连接失败"})})
}



)
module.exports = router;
